package com.github.thinwonton.springboot.showcase.jta.mybatisplus.config;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusProperties;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

import javax.sql.DataSource;

/**
 * jta-orders库数据源配置
 *
 * @author hugo
 * @date 2018/8/21
 */
@Configuration
@MapperScan(
        basePackages = "com.github.thinwonton.springboot.showcase.jta.mybatisplus.user.dao",
        sqlSessionFactoryRef = "jtaUserSqlSessionFactory"
)
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class JTAUserDataSourceConfig {

    private static final String[] MAPPER_LOCATIONS = new String[]{
            "classpath:mapper/user/*.xml"
    };

    private static final String CONFIG_PROPERTIES_PREFIX = "spring.datasource.jta-user";

    private final ResourceLoader resourceLoader;
    private final ApplicationContext applicationContext;
    private final Interceptor[] interceptors;
    private final DatabaseIdProvider databaseIdProvider;
    private final MybatisPlusProperties mybatisPlusProperties;

    @Autowired
    public JTAUserDataSourceConfig(MybatisPlusProperties mybatisPlusProperties,
                                   ResourceLoader resourceLoader,
                                   ApplicationContext applicationContext,
                                   ObjectProvider<Interceptor[]> interceptorsProvider,
                                   ObjectProvider<DatabaseIdProvider> databaseIdProvider) {
        this.mybatisPlusProperties = mybatisPlusProperties;
        this.interceptors = interceptorsProvider.getIfAvailable();
        this.resourceLoader = resourceLoader;
        this.applicationContext = applicationContext;
        this.databaseIdProvider = databaseIdProvider.getIfAvailable();
    }

    @ConfigurationProperties(prefix = CONFIG_PROPERTIES_PREFIX)
    @Bean
    public DruidXADataSource jtaUserDruidXADataSource() {
        return new DruidXADataSource();
    }

    @Bean
    public DataSource jtaUserDataSource() {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(jtaUserDruidXADataSource());
        return atomikosDataSourceBean;
    }

    @Bean(name = "jtaUserSqlSessionFactory")
    public SqlSessionFactory jtaUserSqlSessionFactory() throws Exception {
        //设置mapper路径
        this.mybatisPlusProperties.setMapperLocations(MAPPER_LOCATIONS);

        SqlSessionFactory sqlSessionFactory = MybatisPlusBuilder
                .createSqlSessionFactory(
                        jtaUserDataSource(),
                        this.mybatisPlusProperties,
                        this.resourceLoader,
                        this.applicationContext,
                        this.interceptors,
                        this.databaseIdProvider);
        return sqlSessionFactory;
    }
}
